package com.chen.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.constant.DriverCarConstants;
import com.chen.internalcommon.dto.*;
import com.chen.internalcommon.response.OrderDriverResponse;
import com.chen.servicedriveruser.mapper.CarMapper;
import com.chen.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.chen.servicedriveruser.mapper.DriverUserMapper;
import com.chen.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class DriverUserService {
    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private CarMapper carMapper;


    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    public String testDB() {
        return driverUserMapper.selectById(1).toString();
    }

    /**
     * 添加司机
     * @param driverUser
     * @return
     */
    public ResponseResult addDriverUser(DriverUser driverUser) {
        try {
            LocalDateTime now = LocalDateTime.now();
            driverUser.setGmtCreate(now);
            driverUserMapper.insert(driverUser);
            log.info("司机添加成功");
            // 同时初始化司机工作状态
            DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
            driverUserWorkStatus.setDriverId(driverUser.getId());
            driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
            driverUserWorkStatus.setGmtCreate(now);
            driverUserWorkStatusMapper.insert(driverUserWorkStatus);
            log.info("司机工作状态初始化成功");
        } catch (Exception e) {
            return ResponseResult.fail(e.getMessage());
        }
        return ResponseResult.success();
    }

    /**
     * 更新司机信息
     * @param driverUser
     * @return
     */
    public ResponseResult updateDriverUser(DriverUser driverUser) {
        int update = 0;
        try {
            update = driverUserMapper.updateById(driverUser);
        } catch (Exception e) {
            return ResponseResult.fail(e.getMessage());
        }
        return update == 0 ? ResponseResult.fail("更新失败") : ResponseResult.success();
    }

    /**
     * 根据手机号查询司机信息
     * @param driverPhone
     * @return
     */
    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone) {
        LambdaQueryWrapper<DriverUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverUser::getDriverPhone, driverPhone)
                .eq(DriverUser::getState, DriverCarConstants.DRIVER_STATE_VALID);
        DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
        if (driverUser == null) {
            log.info("司机不存在");
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS);
        }
        return ResponseResult.success(driverUser);
    }

    /**
     * 根据车辆Id查询订单需要的司机信息
     */
    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {
        //  查询车辆是否存在对应司机与之绑定
        LambdaQueryWrapper<DriverCarBindingRelationship> driverCarWrapper = new LambdaQueryWrapper<>();
        driverCarWrapper.eq(DriverCarBindingRelationship::getCarId, carId)
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarWrapper);
        if (driverCarBindingRelationship == null) {
            log.info("car:{}没有对应绑定司机", carId);
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS);
        }
        // 查看对应司机的工作状态是否是 出车 状态
        Long driverId = driverCarBindingRelationship.getDriverId();
        LambdaQueryWrapper<DriverUserWorkStatus> driverUserWorkStatusWrapper = new LambdaQueryWrapper<>();
        driverUserWorkStatusWrapper.eq(DriverUserWorkStatus::getDriverId, driverId)
                .eq(DriverUserWorkStatus::getWorkStatus, DriverCarConstants.DRIVER_WORK_STATUS_START);
        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(driverUserWorkStatusWrapper);
        if (driverUserWorkStatus == null) {
            log.info("司机：{}非出车工作状态", driverId);
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY);
        }
        // 查询司机信息和车辆信息返回
        LambdaQueryWrapper<Car> carWrapper = new LambdaQueryWrapper<>();
        carWrapper.eq(Car::getId, carId);
        Car car = carMapper.selectOne(carWrapper);
        LambdaQueryWrapper<DriverUser> driverUserWrapper = new LambdaQueryWrapper<>();
        driverUserWrapper.eq(DriverUser::getId, driverId);
        DriverUser driverUser = driverUserMapper.selectOne(driverUserWrapper);
        String driverPhone = driverUser.getDriverPhone();
        String licenseId = driverUser.getLicenseId();
        String vehicleNo = car.getVehicleNo();
        String vehicleType = car.getVehicleType();
        OrderDriverResponse orderDriverResponse = new OrderDriverResponse(driverId,
                driverPhone, carId, licenseId, vehicleNo, vehicleType);
        return ResponseResult.success(orderDriverResponse);

    }
}
